package org.jeecg.modules.demo.hd.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.system.entity.AppDeviceFreeze;
import org.jeecg.modules.system.entity.AppUserDeviceLog;
import org.jeecg.modules.system.service.IAppDeviceFreezeService;
import org.jeecg.modules.system.service.IAppUserDeviceLogService;
import org.jeecg.modules.system.vo.UserDeviceLogVo;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 检测用户和设备是否违规
 * 1、检测设备运行日志中是否存在 模拟器情况，出现直接冻结用户和设备ID号   1
 * 2、打开vpn连接状态，冻结设备                                    1
 * 3、打开ADB调试状态，冻结设备                                    1
 * 4、SIM卡状态为N，冻结设备                                      1
 * 5、电池电量连续24小时为100，冻结设备
 * 6、是否运行在多开/分身环境，冻结设备                             1
 * 7、脚本，冻结状态                                             1
 * 8、设备存储异常，冻结设备
 */
@Slf4j
public class DetectingViolationsTaskJob implements Job {

    @Resource
    private IAppUserDeviceLogService logService;

    @Resource
    private IAppDeviceFreezeService deviceFreezeService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        UserDeviceLogVo vo = new UserDeviceLogVo();
        vo.setScript("Y");
        vo.setAdbStatus("Y");
        vo.setSimulator("Y");
        vo.setRunningInVirtualSpace("Y");
        vo.setSimStatus("N");
        vo.setVpnStatus("Y");
        // 查询所有异常设备异常设备
        List<AppUserDeviceLog> list = logService.getList(vo);
        List<AppDeviceFreeze> deviceFreezeList = deviceFreezeService.getList();  // 获取所有异常设备
        // 遍历异常设备列表，添加异常设备清单
        list.stream().map(item -> item.getDeviceId()).distinct().forEach(item -> {
            String deviceId = item;
            // 判断是否已经在异常设备清单，如果存在，跳过
            long count = deviceFreezeList.stream().filter(x -> x.getDeviceId().equals(item)).count();
            if (count > 0)
                return;
            // 如果不存在，添加到异常设备清单
            List<String> androidIds = list.stream().filter(i -> i.getDeviceId().equals(item))
                    .map(x -> x.getAndroidId()).distinct().collect(Collectors.toList());
            String androidIdStr = String.join(",", androidIds);
            AppDeviceFreeze freeze = new AppDeviceFreeze();
            freeze.setFreezeType("1");     // 永久冻结
            freeze.setAndroidId(androidIdStr);
            freeze.setDeviceId(deviceId);
            freeze.setStatus("0");
            freeze.setStartdate(DateUtils.getDate());
            List<String> remarks = new ArrayList<>();
            List<AppUserDeviceLog> data = list.stream().filter(i -> i.getDeviceId().equals(item)).collect(Collectors.toList());
            long adbCount = data.stream().filter(x -> "Y".equals(x.getAdbStatus())).count();
            if (adbCount > 0) {
                remarks.add("开启ADB调试");
            }
            long vpnCount = data.stream().filter(x -> "Y".equals(x.getVpnStatus())).count();
            if (vpnCount > 0) {
                remarks.add("VPN连接");
            }
            long simCount = data.stream().filter(x -> "N".equals(x.getSimStatus())).count();
            if (simCount > 0) {
                remarks.add("无SIM卡");
            }
            long simulatorCount = data.stream().filter(x -> "Y".equals(x.getSimulator())).count();
            if (simulatorCount > 0) {
                remarks.add("疑似模拟器");
            }
            long runCount = data.stream().filter(x -> "Y".equals(x.getRunningInVirtualSpace())).count();
            if (runCount > 0) {
                remarks.add("运行在多开/分身环境中");
            }
            long scriptCount = data.stream().filter(x -> "Y".equals(x.getScript())).count();
            if (scriptCount > 0) {
                remarks.add("疑似脚本");
            }
            freeze.setRemark(String.join(",", remarks));
            deviceFreezeService.save(freeze);
        });
        // 查询电池电量一直为100% 的设备，冻结


        // 查询设备存储空间为80G一下的设备，冻结


        // 更新redis缓存
        deviceFreezeService.cleanRedis();
    }
}
